class Admin::OptionGroupsController < Admin::BaseController

  def new
    #note: its the option id being passed even though we're in the option group controller 
    @option = Option.find(params[:id])
    #messy SQL but there doesn't seem to be an easier way (at least that i know of)
    @all_option_values = OptionValue.find_by_sql(["SELECT * FROM option_values WHERE id NOT IN (SELECT option_value_id FROM option_groups WHERE option_id = ? ORDER BY name)", 
                                                 @option.id])
  end
  
  def create
    @option = Option.find(params[:id])
    @option_value = OptionValue.find(params[:option_value])
    option_group = OptionGroup.new
    option_group.option = @option
    option_group.option_value = @option_value
    option_group.save!
    flash[:notice] = 'Option group was successfully created.'
    redirect_to :controller => 'options', :action => 'edit', :id => @option    
  rescue Exception => e
    logger.error("unable to create/update option group")
    logger.error("message: " + e.message)
    flash[:error] = ' Problem saving option group'
    render :action => 'new'
  end

  def destroy
    group = OptionGroup.find(params[:id])
    option = group.option
    group.destroy
    #OptionGroup.destroy(params[:id])
    flash[:notice] = 'Option group successfully deleted'
    redirect_to :controller => 'options', :action => 'edit', :id => option
  end

end
